package com.seekting.autosetup;

import android.os.RemoteException;
import android.util.Log;

import java.io.IOException;
import java.util.regex.Pattern;

import androidx.test.uiautomator.By;
import androidx.test.uiautomator.UiDevice;
import androidx.test.uiautomator.UiObject2;
import androidx.test.uiautomator.Until;

public class Utils {
    public static final int TIMEOUT = 1500;
    private static final int SLEEP_TIME = 1500;
    private static final boolean DEBUG = BuildConfig.DEBUG;
    private static final String TAG = "Utils";
    //adb shell dumpsys activity |grep -P "mResumedActivity:|mFocusedActivity"| awk '{print $4}'
    private static final String top = "dumpsys activity";
    /**
     * (mResumedActivity|mFocusedActivity): ActivityRecord\{[^\s]* [^\s]* ([^\s]*)
     * 6.0
     * mFocusedActivity: ActivityRecord{4b5ac53 u0 com.android.launcher3/.Launcher t856}
     * mResumedActivity: ActivityRecord{44a7af2 u0 com.android.browser/.BrowserActivity t29}
     */
    private static final Pattern activityPattern = Pattern.compile("mResumedActivity: ActivityRecord\\{[^\\s]* [^\\s]* ([^\\s]*)");


    public static String getTopActivity(UiDevice uiDevice) {
        String str = executeShellCommand(uiDevice, top);
        return PatternGroupFinder.findGroupOne(activityPattern, str);

    }

    public static void closeAPP(UiDevice uiDevice, String sPackageName) {
        Log.i(TAG, "closeAPP: ");
        executeShellCommand(uiDevice, "am force-stop " + sPackageName);//通过命令行关闭app
    }

    public static void wakeup(UiDevice uiDevice) {
        try {
            if (!uiDevice.isScreenOn()) {

                uiDevice.wakeUp();
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }


    public void startAPP(UiDevice uiDevice, String sPackageName, String sLaunchActivity) {
        executeShellCommand(uiDevice, "am start -n " + sPackageName + "/" + sLaunchActivity);//通过命令行启动app
    }

    public static String executeShellCommand(UiDevice uiDevice, String cmd) {
        try {
            return uiDevice.executeShellCommand(cmd);//通过命令行启动app
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }

    public static boolean clickById(UiDevice device, String id) {
        UiObject2 UiObject2 = findById(device, id);
        return click(UiObject2);

    }

    public static UiObject2 findById(UiDevice device, String id) {
        UiObject2 uiObject2 = device.wait(Until.findObject(By.res(id)), TIMEOUT);
        return uiObject2;
    }

    public static UiObject2 findById(UiDevice device, String id, int depth) {
        if (depth < 0) {
            return findById(device, id);
        } else {
            UiObject2 uiObject2 = device.wait(Until.findObject(By.res(id).depth(depth)), TIMEOUT);
            return uiObject2;
        }
    }

    public static UiObject2 findByIdAndText(UiDevice uiDevice, String id, String text) {
        return uiDevice.wait(Until.findObject(By.res(id).text(text)), TIMEOUT);
    }

    public static boolean setTextById(UiDevice device, String id, String text) {
        UiObject2 uiObject2 = findById(device, id);
        return setText(uiObject2, text);
    }

    public static boolean setTextById(UiDevice device, String id, int depth, String text) {
        UiObject2 uiObject2 = findById(device, id, depth);
        return setText(uiObject2, text);
    }

    public static boolean setText(UiObject2 uiObject2, String text) {
        if (uiObject2 == null) {
            return false;
        }
        try {
            uiObject2.setText(text);
            return true;
        } catch (androidx.test.uiautomator.StaleObjectException e) {

        }
        return false;
    }

    public static boolean clickRect(UiDevice uiDevice, UiObject2 uiObject2) {
        if (uiObject2 == null) {
            return false;
        }
        try {
            uiDevice.click(uiObject2.getVisibleBounds().centerX(), uiObject2.getVisibleBounds().centerY());
            return true;
        } catch (androidx.test.uiautomator.StaleObjectException e) {

        }
        return false;
    }

    public static boolean parentClick(UiObject2 uiObject) {
        if (uiObject != null) {
            UiObject2 parent = uiObject.getParent();
            return click(parent);

        }
        return false;
    }

    public static boolean click(UiObject2 uiObject2) {
        if (uiObject2 != null && uiObject2.isClickable()) {
            try {
                uiObject2.click();
                return true;
            } catch (androidx.test.uiautomator.StaleObjectException e) {

            }
        }
        return false;
    }

    public static void sleep() {
        sleep(SLEEP_TIME);
    }

    public static void sleep(int time) {
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void waitForIdle(UiDevice device) {
        device.waitForIdle(TIMEOUT);
    }

    public static void waitForWindowUpdate(UiDevice device, String pkg) {
        device.waitForWindowUpdate(pkg, TIMEOUT);
    }
}
